from fastapi import FastAPI

"""2.路径参数"""
app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    """
    1、把路径参数 item_id 的值传递给路径函数的参数 item_id
    2、声明路径参数的类型
    """
    return {"item_id": item_id}


# http://127.0.0.1:8000/items/foo
# http://127.0.0.1:8000/items/3

@app.get("/users/me")
async def read_user_me():
    return {"user_id": "the current user"}


@app.get("/users/{user_id}")
async def read_user(user_id: str):
    return {"user_id": user_id}


# 创建预设值
from enum import Enum


class ModelName(str, Enum):
    alexnet = 'alexnet'
    resnet = 'resnet'
    lenet = 'lenet'


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    # 比较枚举元素 is
    if model_name is ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}
    # 获取枚举值  使用 ModelName.lenet.value 也能获取值 "lenet"。
    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}
    return {"model_name": model_name, "message": "Have some residuals"}


@app.get("/files/{file_patt:path}")
async def read_file(file_patt: str):
    return {"file_patt": file_patt}
